﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Yanesdk.Draw;
using System.IO;

namespace ADV_Evaluator.Data
{
	public static class DataConverter
	{
		public static byte[] DataEncode(string filename)
		{
			Surface face = new Surface();
			face.Load(filename);
			byte[] width =  BitConverter.GetBytes(face.Width);
			byte[] height =  BitConverter.GetBytes(face.Height);
			byte[] fs = new byte[8 + (face.Width * face.Height * 4)];
			int i = 0;
			fs[i] = width[0]; i++;
			fs[i] = width[1]; i++;
			fs[i] = width[2]; i++;
			fs[i] = width[3]; i++;
			fs[i] = height[0]; i++;
			fs[i] = height[1]; i++;
			fs[i] = height[2]; i++;
			fs[i] = height[3]; i++;
			for (int y = 0; y < face.Height; y++)
			{
				for (int x = 0; x < face.Width; x++)
				{
					Color4ub col = face.GetPixel(x,y);
					fs[i] = col.R; i++;
					fs[i] = col.G; i++;
					fs[i] = col.B; i++;
					fs[i] = col.A; i++;
				}
			}

			return fs;
		}

		public static Bitmap DataDecode(string filename)
		{
			FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
			byte[] byt = new byte[fs.Length];
			fs.Read(byt, 0, Convert.ToInt32(fs.Length));
			int i = 0;
			int width = BitConverter.ToInt32(byt, 0);
			int height = BitConverter.ToInt32(byt, 4);
			Bitmap bit = new Bitmap(width, height);
			for (int y = 0; y < height; y++)
			{
				for (int x = 0; x < width; x++)
				{
					int r = Convert.ToInt32(byt[i]); i++;
					int g = Convert.ToInt32(byt[i]); i++;
					int b = Convert.ToInt32(byt[i]); i++;
					int a = Convert.ToInt32(byt[i]); i++;
					Color col = Color.FromArgb(a, r, g, b);
					bit.SetPixel(x, y, col);
				}
			}
			fs.Close();
			fs.Dispose();

			return bit;
		}
	}
}
